<?php
/**
 * Created by PhpStorm.
 * User: yuqing
 * Date: 16-4-17
 * Time: 上午9:17
 */
class Position
{

    /**
     * 根据参数返回array('A301'=>'63445388')的sku储位关联数组
     * @param $sku
     * @return array
     */
    public static function getSkusPostion($sku){
        $ware_id  = Kohana::$config->load('warehouse')->warehouse_id;
        $sql = "SELECT
                    sps.sku 'sku', sp.place 'place'
                FROM
                    skusystem_place_skus sps
                        LEFT JOIN
                    skusystem_places sp ON sps.place_id = sp.id
                WHERE
                    sps.isavailable = 1 AND sp.warehouse_id = $ware_id ";
        if(is_array($sku)){
            $sql .= " AND sps.sku IN ('".join("','",$sku)."') ";
        }else if(is_string($sku)){
            $sql .= " AND sps.sku = '$sku'";
        }
        $skuPositionArr = DB::query(Database::SELECT,$sql)
                                ->execute('newerp')
                                ->as_array('sku','place');
        if($skuPositionArr){
            return $skuPositionArr;
        }else{
            return false;
        }
    }

    /**
     *
     * @param $input
     * @param $columnKey
     * @param null $indexKey
     * @return array
     */
    public static function array_columns($input,$columnKey,$indexKey = NULL)
    {
        $columnKeyIsNumber = (is_numeric($columnKey))?TRUE:FALSE;
        $indexKeyIsNull    = (is_null($indexKey))?TRUE:FALSE;
        $indexKeyIsNumber  = (is_numeric($indexKey))?TRUE:FALSE;
        $result            = array();

        foreach((array)$input AS $key => $row){
            if($columnKeyIsNumber){
                $tmp = array_slice($row,$columnKey,1);
                $tmp = (is_array($tmp)&&!empty($tmp))?current($tmp):NULL;
            }else{
                $tmp = isset($row[$columnKey])?$row[$columnKey]:NULL;
            }
            if(!$indexKeyIsNull){
                if($indexKeyIsNumber){
                    $key = array_slice($row,$indexKey,1);
                    $key = (is_array($key)&&!empty($key))?current($key):NULL;
                    $key = is_null($key)?0:$key;
                }else{
                    $key = isset($row[$indexKey])?$row[$indexKey]:0;
                }
            }

            $result[$key] = $tmp;
        }

        return $result;
    }

}